/**
 * 
 */
package com.nextive.interview.activity;

import com.nextive.interview.R;
import com.nextive.interview.service.UserInfoService;
import com.nextive.interview.util.PhoneContactsUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author Cesar Roman
 *
 */
public class CreateNewContactActivity extends Activity {

	private EditText contactName;
	private EditText contactEmail;
	private Button confirmButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_contact);
		
		bindInputs();
		configureConfirmButton();
		
		
	}
	
	private void bindInputs() {
		
		contactName = (EditText) findViewById(R.id.new_contact_name);
		contactEmail = (EditText) findViewById(R.id.new_contact_email);
		confirmButton = (Button) findViewById(R.id.confirm_create_contact);
		
	}
	
	private void configureConfirmButton() {

		confirmButton.setOnClickListener(new CreateContactClickListener(this));

	}

	private final class CreateContactClickListener implements OnClickListener {

		private Context context;
		
		public CreateContactClickListener(Context context) {
			
			this.context = context;
			
		}
		
		@Override
		public void onClick(View v) {

			String name = contactName.getText().toString();
			String email = contactEmail.getText().toString();
			
			boolean result = PhoneContactsUtil.createNewPhoneContactBatch(context, name, email);
			if (!result) {
				
				setResult(Activity.RESULT_CANCELED);
				
			} else {
				
				Intent intent = new Intent();
				intent.putExtra(UserInfoService.USER_INFO_KEYS.EMAIL.name(), email);
				intent.putExtra(UserInfoService.USER_INFO_KEYS.NAME.name(), name);
				setResult(RESULT_OK, intent);
				
			}
				
			finish();

		}
		
	}

}
